/*
 * @Author       : xh
 * @Date         : 2022-10-21 14:47:13
 * @LastEditors: y001554
 * @FileName     :
 */

import '@/styles/index.scss' // global css
import Cookies from 'js-cookie'
import { qiankunWindow, renderWithQiankun } from 'vite-plugin-qiankun/dist/helper'
import { createApp } from 'vue'
import App from './App.vue'
import './permission'
import router from './router'
//import element-plus
import { useAppStore } from '@/store/app'
import ylPlugins from '@loserjohn/plugins'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import '@loserjohn/plugins/lib/style.css'

console.log('ylPlugins', ylPlugins)
// import zhCn from 'element-plus/es/locale/lang/zh-cn'

// global mixin
// import elementMixin from '@/mixins/elementMixin'
// app.mixin(elementMixin)
// import commonMixin from '@/mixins/commonMixin'
// app.mixin(commonMixin)
// import routerMixin from '@/mixins/routerMixin'
// app.mixin(routerMixin)
// //import axios req
// import axiosReq from '@/utils/axiosReq'
// app.config.globalProperties.$axiosReq = axiosReq

//svg-icon
//import svg-icon doc in  https://github.com/anncwb/vite-plugin-svg-icons/blob/main/README.zh_CN.md
import svgIcon from '@/icons/SvgIcon.vue'
// import request from '@/utils/requests.js';
import request from '@/utils/requests'

// import request from '@/utils/test';
import 'virtual:svg-icons-register'

//global mount moment-mini
// import $momentMini from 'moment-mini'
// app.config.globalProperties.$momentMini = $momentMini
//import global directive（unplugin-vue-components auto scan）
import directive from '@/directives'

//import router intercept
let app = null
// app.config.globalProperties.$request = request
//element svg icon(unplugin-vue-components auto scan)
// import ElSvgIcon from '@/components/ElSvgIcon.vue'
// app.component('ElSvgIcon', ElSvgIcon)

//error log  collection
import errorLog from '@/hooks/useErrorLog'
errorLog()

//import theme
import './theme/index.scss'
//import unocss
import 'uno.css'

//pinia
import { createPinia } from 'pinia'

// app.use(createPinia())

// app.use(router).mount('#app')

// 挂载函数
function render(props) {
  app = createApp(App)

  app.config.globalProperties.$request = request
  app.component('SvgIcon', svgIcon)
  app.use(ElementPlus)
  directive(app)
  // const { container } = props;
  app.use(createPinia())
  app.use(router)

  if (props.microMode) {
    const appStore = useAppStore()
    const opened = Cookies.get('sidebarStatus') == 0 ? false : true
    appStore.M_sidebar_opened(opened)

    // actions.setActions(props)
    app.config.globalProperties.$microMode = props.microMode || false
    app.config.globalProperties.$onGlobalStateChange = props.onGlobalStateChange
    app.config.globalProperties.$setGlobalState = props.setGlobalState
  }

  // app.mount(container ? container.querySelector('#indexContainer') : '#indexContainer'); // 这个ID为index.html中div的ID名称
  app.use(router).use(ylPlugins).mount('#mc_app')
}

renderWithQiankun({
  mount(props) {
    console.log('子应用', props)
    render({ microMode: true, ...props })

    // const opened = computed(() => {
    //   return appStore.sidebar.opened
    // })

    // store.commit("updateUserInfo",props);
    // 在vue全局注册主应用的状态监听和状态改变事件
  },
  bootstrap() {
    console.log('bootstrap')
  },
  unmount() {
    console.log('unmount')
    app.unmount()
    app = null
  },
  update() {
    console.log('update')
  }
})

if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  console.log('非乾坤')
  render({})
}
